package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysMd;
import com.ruoyi.system.mapper.SysMdMapper;
import com.ruoyi.system.service.ISysMdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author renzhihao
 * @date 2024/01/31 16:49
 */
@Service
public class SysMdServiceImpl implements ISysMdService {

    @Autowired
    private SysMdMapper mdMapper;

    @Override
    public List<SysMd> selectMdList(SysMd sysMd) {
        return mdMapper.selectMdList(sysMd);
    }

    @Override
    public int insertMd(SysMd sysMd) {
        sysMd.setContent(URLDecoder.decode(sysMd.getContent(), StandardCharsets.UTF_8));
        return mdMapper.insertMd(sysMd);
    }

    @Override
    public SysMd selectMdById(Long mdId) {
        return mdMapper.selectMdById(mdId);
    }

    @Override
    public int updateMd(SysMd sysMd) {
        sysMd.setContent(URLDecoder.decode(sysMd.getContent(), StandardCharsets.UTF_8));
        return mdMapper.updateMd(sysMd);
    }

    @Override
    public int deleteMdByIds(Long[] ids) {
        return mdMapper.deleteMdByIds(ids);
    }

    @Override
    public boolean checkMdNameUnique(SysMd sysMd) {
        Long mdId = StringUtils.isNull(sysMd.getMdId()) ? -1L : sysMd.getMdId();
        SysMd info = mdMapper.checkMdNameUnique(sysMd.getMdName());
        if (StringUtils.isNotNull(info) && info.getMdId().longValue() != mdId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
